#include <bits/stdc++.h>
using namespace std;
int main() {
    int n, ans = 0;
    cin >> n;
    vector<vector<int>> a(n, vector<int>(3));
    vector<vector<int>> q;
    for (int i = 0; i < n; i++) {
        for (int j = 0; j < 3; j++) {
            cin >> a[i][j];
        }
        int f = a[i][0];
        int v = a[i][1];
        int t = a[i][2];
        q.erase(remove_if(q.begin(), q.end(), 
            [&](const vector<int>& x) { 
                return x[1] < t; 
            }), 
            q.end()); 
        if (f == 0) {
            ans += v;
            q.push_back({v, t + 45});
        } else {
            bool found = 0;
            for (auto it = q.begin(); it != q.end(); it++) {
                if ((*it)[0] >= v) {
                    q.erase(it);
                    found = 1;
                    break;
                }
            }
            if (!found) {
                ans += v;
            }
        }
    }
    cout << ans << endl;
    return 0;
}
